package com.ruoyi.location.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.location.mapper.TBaseSpaceLocationMapper;
import com.ruoyi.location.domain.TBaseSpaceLocation;
import com.ruoyi.location.service.ITBaseSpaceLocationService;

/**
 * 空间位置Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-29
 */
@Service
public class TBaseSpaceLocationServiceImpl implements ITBaseSpaceLocationService
{
    @Autowired
    private TBaseSpaceLocationMapper tBaseSpaceLocationMapper;

    /**
     * 查询空间位置
     *
     * @param id 空间位置主键
     * @return 空间位置
     */
    @Override
    public TBaseSpaceLocation selectTBaseSpaceLocationById(Long id)
    {
        return tBaseSpaceLocationMapper.selectTBaseSpaceLocationById(id);
    }

    /**
     * 查询空间位置列表
     *
     * @param tBaseSpaceLocation 空间位置
     * @return 空间位置
     */
    @Override
    public List<TBaseSpaceLocation> selectTBaseSpaceLocationList(TBaseSpaceLocation tBaseSpaceLocation)
    {
        return tBaseSpaceLocationMapper.selectTBaseSpaceLocationList(tBaseSpaceLocation);
    }

    /**
     * 新增空间位置
     *
     * @param tBaseSpaceLocation 空间位置
     * @return 结果
     */
    @Override
    public int insertTBaseSpaceLocation(TBaseSpaceLocation tBaseSpaceLocation)
    {
        return tBaseSpaceLocationMapper.insertTBaseSpaceLocation(tBaseSpaceLocation);
    }

    /**
     * 修改空间位置
     *
     * @param tBaseSpaceLocation 空间位置
     * @return 结果
     */
    @Override
    public int updateTBaseSpaceLocation(TBaseSpaceLocation tBaseSpaceLocation)
    {
        return tBaseSpaceLocationMapper.updateTBaseSpaceLocation(tBaseSpaceLocation);
    }

    /**
     * 批量删除空间位置
     *
     * @param ids 需要删除的空间位置主键
     * @return 结果
     */
    @Override
    public int deleteTBaseSpaceLocationByIds(Long[] ids)
    {
        return tBaseSpaceLocationMapper.deleteTBaseSpaceLocationByIds(ids);
    }

    /**
     * 删除空间位置信息
     *
     * @param id 空间位置主键
     * @return 结果
     */
    @Override
    public int deleteTBaseSpaceLocationById(Long id)
    {
        return tBaseSpaceLocationMapper.deleteTBaseSpaceLocationById(id);
    }

    @Override
    public int deleteUpdate(Long ids) {
        List<TBaseSpaceLocation> list = tBaseSpaceLocationMapper.list();
        Long[] id = new Long[list.size()];
        int i = 0;
        for (TBaseSpaceLocation location : list) {
            if (location.getId().equals(ids)){
                id = deleteChildren(location,list,id,1);
                id[i] = location.getId();
            }
        }
        tBaseSpaceLocationMapper.deleteUpdate(id);
        return 1;
    }

    private Long[] deleteChildren(TBaseSpaceLocation location, List<TBaseSpaceLocation> list, Long[] id,int i) {
        for (TBaseSpaceLocation location1 : list) {
            if (location1.getPid().equals(location.getId())){
                id[i++] = location1.getId();
                deleteChildren(location1,list,id,i);
            }
        }
        return id;
    }
}